<?php

namespace App\Model\Member;

use App\Component\DeviceIdentity\DeviceUser;
use App\Component\User;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Output;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;

/**
 * CREATE TABLE `ss_member_profit_score` (
 * `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 * `user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户id',
 * `score` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '变动金额',
 * `before_score` decimal(10,2) NOT NULL COMMENT '变动前金额',
 * `after_score` decimal(10,2) NOT NULL COMMENT '变动后金额',
 * `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
 * `type` char(20) NOT NULL DEFAULT '' COMMENT '变动类型 USERPROFIT:个人收益;REFFERERPROFIT:团队收益',
 * `source_id` int(11) NOT NULL DEFAULT '0' COMMENT '关联用户ID',
 * `object_id` varchar(1000) NOT NULL DEFAULT '' COMMENT '关联ID',
 * `created_date` date NOT NULL DEFAULT '1970-01-01' COMMENT '创建日期',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE,
 * KEY `idx_user_id` (`user_id`) USING BTREE,
 * KEY `idx_object_id` (`object_id`(768)) USING BTREE
 * ) ENGINE=InnoDB AUTO_INCREMENT=20441 DEFAULT CHARSET=utf8mb4 COMMENT='收益账户变动流水';
 */
/**
 * Class MemberModel
 * @property integer $id
 * @property integer $user_id
 * @property integer $score
 * @property integer $before_score
 * @property integer $after_score
 * @property string $remark
 * @property string $type
 * @property string $source_id
 * @property string $object_id
 * @property string $created_date
 * @property string $created_at
 */
class MemberProfitScoreModel extends BaseModel
{

    protected $tableName = 'ss_member_profit_score';

    protected $casts = [
        'id'         => 'integer',
        'user_id'    => 'integer',
        'score'      => 'float',
        'before_score' => 'float',
        'after_score' => 'float',
        'remark'     => 'string',
        'type'       => 'string',
        'source_id'  => 'integer',
        'object_id'  => 'string',
        'created_date' => 'string',
        'created_at' => 'string',
    ];

    const TYPE_CASHOUT = 'CASHOUT';//提现
    const TYPE_CASHOUT_CANCEL = 'CASHOUTCANCEL';//取消提现
    const TYPE_USER_PROFIT = 'USERPROFIT';//个人收益
    const TYPE_REFFERER_PROFIT = 'REFFERERPROFIT';//团队收益
    const ARCHIVE_RECORD_LIMIT = 1000; //存档记录条数

    public static function getTypeDict(?string $key = null)
    {
        return self::getMap([
            self::TYPE_USER_PROFIT     => '个人收益',
            self::TYPE_REFFERER_PROFIT => '团队收益',
            self::TYPE_CASHOUT         => '提现',
            self::TYPE_CASHOUT_CANCEL  => '取消提现',
        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('memberProfitScoreCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('memberProfitScoreCache:');
    }
}
